package lc.q1_50;

import java.util.*;

public class Q49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> results = new HashMap<>();
        int[] count = new int[26];
        for (String str : strs) {
            Arrays.fill(count,0);
            for (char c : str.toCharArray()) {
                count[c - 'a'] ++;
            }
            StringBuilder sb = new StringBuilder();
            for (int i : count) {
                sb.append("#");
                sb.append(i);
            }
            String key = sb.toString();
            if (!results.containsKey(key)) results.put(key,new ArrayList<>());
            results.get(key).add(str);
        }
        return new ArrayList<>(results.values());
    }

    public static void main(String[] args) {
        String[] strings = {
                "eat", "tea", "tan", "ate", "nat", "bat"
        };
        System.out.println(new Q49().groupAnagrams(strings));
    }

}
